package com.weitu.risk.controller;

import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.base.controller.BaseController;
import com.weitu.hiddenDanger.entity.HiddenDangerInfo;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.risk.entity.MatrixLevel;
import com.weitu.risk.entity.MatrixPossibility;
import com.weitu.risk.entity.MatrixResult;
import com.weitu.risk.entity.RiskLevel;
import com.weitu.risk.service.MatrixLevelService;
import com.weitu.risk.service.MatrixPossibilityService;
import com.weitu.risk.service.MatrixResultService;
import com.weitu.risk.service.RiskLevelService;
import com.weitu.util.AjaxResult;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * 矩阵维护Controller
 * Created by fanyuting on 2017/4/24 0024.
 */
@Controller
@RequestMapping("risk/matrix")
public class MatrixController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH+"risk/matrix/";
    @Autowired
    private RiskLevelService riskLevelService;
    @Autowired
    private MatrixPossibilityService possibilityService;
    @Autowired
    private MatrixResultService  resultService;
    @Autowired
    private MatrixLevelService matrixLevelService;
    @Autowired
    private AccessoryUtilService accessoryUtilService;//附件
    /*===================================风险等级维护controller==========================================*/
    /**
     *  跳转到风险等级维护页面
     */
    @RequestMapping("/level/list")
    public String toLevelPage(){
        return MODULE_BATH_PATH + "level/list";
    }
    /**
     * 查询风险等级列表信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("queryLevelListByParam")
    public Object queryLevelList(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String,Object>();
        //获取前台参数
        putParametersIntoMap(map);
        //Integer totalNum = riskLevelService.queryRiskLevelListCountByParam(map);//获取总数
        //DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);
        // PageView pageView = pagination.getPageView();//生成pageView分页对象
        //map.put("pageView", pageView);
        List<RiskLevel> list =  riskLevelService.queryRiskLevelListByParam(map);
        return  list;
    }
    /**
     * 跳转到新增风险等级页面
     * @return
     */
    @RequestMapping("/toAddLevelPage")
    public String toAddLevelPage(){
        return MODULE_BATH_PATH +"level/add";
    }
    /**
     * 跳转到编辑风险等级页面
     * @return
     */
    @RequestMapping("/toEditLevelPage")
    public String toEditLevelPage(HttpServletRequest request, Model model){
        String businessId = request.getParameter("businessId");
        Map<String,Object> map = new HashedMap();
        map.put("id",businessId);
        RiskLevel level = this.riskLevelService.queryRiskLevelById(map);
        model.addAttribute("level",level);
        return MODULE_BATH_PATH +"level/edit";
    }
    /**
     * 新增风险等级信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/addRiskLevelInfo")
    @Transactional
    public AjaxResult addRiskLevelInfo(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap();
        getAddBasicFieldMap(map);
        boolean flag = this.riskLevelService.addRiskLevel(map);
        if(flag){
            return success("风险等级信息保存成功！");
        }
        return fail("风险等级信息保存失败！");
    }
    /**
     * 编辑风险等级信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/editRiskLevelInfo")
    @Transactional
    public AjaxResult editRiskLevelInfo(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap();
        getEditBasicFieldMap(map);
        boolean flag = this.riskLevelService.editRiskLevelById(map);
        if(flag){
            return success("风险等级信息更新成功！");
        }
        return fail("风险等级信息更新失败！");
    }
    /**
     * 删除风险等级信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteRiskLevelInfo")
    @Transactional
    public AjaxResult deleteRiskLevelInfo(HttpServletRequest request, HttpServletResponse response){
       // Map<String,Object> map = new HashMap();
        //getAddBasicFieldMap(map);
        String businessId = request.getParameter("businessId");
        boolean flag = this.riskLevelService.deleteRiskLevelById(businessId);
        if(flag){
            return success("风险等级信息更新成功！");
        }
        return fail("风险等级信息更新失败！");
    }

    /*====================================风险可能性维护controller==================================*/
    /**
     *  跳转到风险可能性维护页面
     */
    @RequestMapping("/possibility/list")
    public String toMatrixPossibilityPage(){
        return MODULE_BATH_PATH + "possibility/list";
    }
    /**
     * 查询风险等级列表信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("queryPossibilityList")
    public Object queryPossibilityList(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String,Object>();
        //获取前台参数
        putParametersIntoMap(map);
        //Integer totalNum = riskLevelService.queryRiskLevelListCountByParam(map);//获取总数
        //DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);
        // PageView pageView = pagination.getPageView();//生成pageView分页对象
        //map.put("pageView", pageView);
        List<MatrixPossibility> list =  this.possibilityService.queryListByParam(map);
        return  list;
    }
    /**
     * 跳转到新增风险可能性页面
     * @return
     */
    @RequestMapping("/toAddPossibilityPage")
    public String toAddPossibilityPage(){
        return MODULE_BATH_PATH +"possibility/add";
    }
    /**
     * 跳转到编辑风险可能性等级页面
     * @return
     */
    @RequestMapping("/toEditPossibilityPage")
    public String toEditPossibilityPage(HttpServletRequest request, Model model){
        String businessId = request.getParameter("businessId");
        Map<String,Object> map = new HashedMap();
        map.put("id",businessId);
        MatrixPossibility possibility = this.possibilityService.queryPossbilityById(businessId);
       // RiskLevel level = this.riskLevelService.queryRiskLevelById(map);
        model.addAttribute("entity",possibility);
        return MODULE_BATH_PATH +"possibility/edit";
    }
    /**
     * 新增风险可能性信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/addRiskPossibilityInfo")
    @Transactional
    public AjaxResult addRiskPossibilityInfo(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap();
        getAddBasicFieldMap(map);
        boolean flag = this.possibilityService.addPossibility(map);
        if(flag){
            return success("风险等级信息保存成功！");
        }
        return fail("风险等级信息保存失败！");
    }
    /**
     * 编辑风险等级信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/editPossibilityInfo")
    @Transactional
    public AjaxResult editPossibilityInfo(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap();
        getEditBasicFieldMap(map);
        boolean flag = this.possibilityService.editPossibility(map);
        if(flag){
            return success("风险可能性等级信息更新成功！");
        }
        return fail("风险可能性等级信息更新失败！");
    }
    /**
     * 删除风险等级信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/deletePossibilityInfo")
    @Transactional
    public AjaxResult deletePossibilityInfo(HttpServletRequest request, HttpServletResponse response){
        // Map<String,Object> map = new HashMap();
        //getAddBasicFieldMap(map);
        String businessId = request.getParameter("businessId");
        boolean flag = this.possibilityService.deletePossibility(businessId);
        if(flag){
            return success("风险可能性等级信息更新成功！");
        }
        return fail("风险可能性等级信息更新失败！");
    }


    /*====================================风险后果维护===========================================*/

    /**
     *  跳转到风险后果维护页面
     */
    @RequestMapping("/result/list")
    public String toMatrixResultPage(){
        return MODULE_BATH_PATH + "result/list";
    }
    /**
     * 查询风险后果列表信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("queryResultList")
    public Object queryResultList(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String,Object>();
        //获取前台参数
        putParametersIntoMap(map);
        //Integer totalNum = riskLevelService.queryRiskLevelListCountByParam(map);//获取总数
        //DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);
        // PageView pageView = pagination.getPageView();//生成pageView分页对象
        //map.put("pageView", pageView);
        List<MatrixResult> list =  this.resultService.queryListByParam(map);
        return  list;
    }
    /**
     * 跳转到新增风险后果页面
     * @return
     */
    @RequestMapping("/toAddResultPage")
    public String toAddResultPage(){
        return MODULE_BATH_PATH +"result/add";
    }
    /**
     * 跳转到编辑风险后果页面
     * @return
     */
    @RequestMapping("/toEditResultPage")
    public String toEditResultPage(HttpServletRequest request, Model model){
        String businessId = request.getParameter("businessId");
        Map<String,Object> map = new HashedMap();
        map.put("id",businessId);
        MatrixResult result = this.resultService.queryResultById(businessId);
        // RiskLevel level = this.riskLevelService.queryRiskLevelById(map);
        model.addAttribute("entity",result);
        return MODULE_BATH_PATH +"result/edit";
    }
    /**
     * 新增风险后果信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/addRiskResultInfo")
    @Transactional
    public AjaxResult addRiskResultInfo(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap();
        getAddBasicFieldMap(map);
        boolean flag = this.resultService.addResult(map);
        if(flag){
            return success("风险后果信息保存成功！");
        }
        return fail("风险后果信息保存失败！");
    }
    /**
     * 编辑风险后果信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/editResultInfo")
    @Transactional
    public AjaxResult editResultInfo(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap();
        getEditBasicFieldMap(map);
        boolean flag = this.resultService.editResult(map);
        if(flag){
            return success("风险后果信息更新成功！");
        }
        return fail("风险后果信息更新失败！");
    }
    /**
     * 删除风险后果信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteResultInfo")
    @Transactional
    public AjaxResult deleteResultInfo(HttpServletRequest request, HttpServletResponse response){
        // Map<String,Object> map = new HashMap();
        //getAddBasicFieldMap(map);
        String businessId = request.getParameter("businessId");
        boolean flag = this.resultService.deleteResult(businessId);
        if(flag){
            return success("风险后果信息更新成功！");
        }
        return fail("风险后果信息更新失败！");
    }

    /*==============================================风险矩阵维护======================================================*/
    /**
     * 跳转到风险矩阵维护
     * @return
     */
    @RequestMapping("/toMatrixManagePage")
    public String toMatrixManagePage(Model model){
        Map<String,Object> map = new HashedMap();
        //查询发生的可能性数据
        List<MatrixPossibility> possibilities = this.possibilityService.queryListByParam(map);
        //查询风险后果数据
        List<MatrixResult> results = this.resultService.queryListByParam(map);
        //查询风险等级数据
        List<RiskLevel> levels = this.riskLevelService.queryRiskLevelListByParam(map);
        //查询矩阵等级数据
        List<MatrixLevel> matrixLevels = this.matrixLevelService.queryMatrixLevels(map);
        model.addAttribute("possibilities", possibilities);
        model.addAttribute("results", results);
        model.addAttribute("levels", levels);
        model.addAttribute("matrixLevels",matrixLevels);
        return MODULE_BATH_PATH + "manage/edit";
    }
    /**
     * 跳转到风险矩阵选择界面
     * @return
     */
    @RequestMapping("/toMatrixPage")
    public String toMatrixPage(Model model){
        Map<String,Object> map = new HashedMap();
        //查询发生的可能性数据
        List<MatrixPossibility> possibilities = this.possibilityService.queryListByParam(map);
        //查询风险后果数据
        List<MatrixResult> results = this.resultService.queryListByParam(map);
        //查询风险等级数据
        List<RiskLevel> levels = this.riskLevelService.queryRiskLevelListByParam(map);
        //查询矩阵等级数据
        List<MatrixLevel> matrixLevels = this.matrixLevelService.queryMatrixLevels(map);
        model.addAttribute("possibilities", possibilities);
        model.addAttribute("results", results);
        model.addAttribute("levels", levels);
        model.addAttribute("matrixLevels",matrixLevels);
        return MODULE_BATH_PATH + "manage/show";
    }

    /**
     * 新增风险矩阵等级信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/addRiskMatrixLevelInfo")
    @Transactional
    public AjaxResult addRiskMatrixLevelInfo(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap();
        getAddBasicFieldMap(map);
        boolean flag = this.matrixLevelService.addOrEditMatrixLevel(map);
        if(flag){
            return success("风险矩阵等级信息保存成功！");
        }
        return fail("风险矩阵等级信息保存失败！");
    }

}
